﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Guild
    {
        string[,] _guild;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._guild = Query.SelectQuery(this.conn, "guild", column);
        }

        public string[,] Guilds
        {
            get { return this._guild; }
        }

        public string[] getGuildInfo(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[] guild = new string[this._guild.GetLength(1)];
            for (int i = 0; i < guild.Length; i++)
            {
                guild[i] = this._guild[id - 1, i];
            }

            return guild;
        }

        public int getGuildLevel(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int level;

            Int32.TryParse(this._guild[id - 1, 2], out level);

            return level;
        }

        public int getGuildPoints(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int points;

            Int32.TryParse(this._guild[id - 1, 3], out points);

            return points;
        }

        public string getGuildNewsT(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return this._guild[id - 1, 4];
        }

        public string getGuildNewsM(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return this._guild[id - 1, 5];
        }

        public int getMembersTotal(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int members;

            Int32.TryParse(this._guild[id - 1, 6], out members);

            return members;
        }

        public int getGuildSlots(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int slots;

            Int32.TryParse(this._guild[id - 1, 7], out slots);

            return slots;
        }

        public int getGuildGold(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int gold;

            Int32.TryParse(this._guild[id - 1, 8], out gold);

            return gold;
        }

        public void setGuildLevel(int id, int level)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_level", level.ToString(), "id=" + id);
        }

        public void setGuildPoints(int id, int points)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_points", points.ToString(), "id=" + id);
        }

        public void setGuildNewsT(int id, string content)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_news_t", content, "id=" + id);
        }

        public void setGuildNewsM(int id, string content)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_news_m", content, "id=" + id);
        }

        public void setGuildMembersT(int id, int members)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_members_t", members.ToString(), "id=" + id);
        }

        public void setGuildStorageSlots(int id, int slots)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_storage_slots", slots.ToString(), "id=" + id);
        }

        public void setGuildGold(int id, int gold)
        {
            Query.UpdateQuery(this.conn, "guild", "guild_war_gold_r", gold.ToString(), "id=" + id);
        }

        public void deleteGuild(int id)
        {
            Query.DeleteQuery(this.conn, "guild", "id=" + id);
        }

        public void addGuild(string name, int level, int points, string news_t, string news_m, int members_t, int slots, int gold)
        {
            string[] columns = { "guild_name", "guild_level", "guild_points", "guild_news_t", "guild_news_m", "guild_members_t", "guild_storage_slots", "guild_war_gold_r" };
            string[] values = { name, level.ToString(), points.ToString(), news_t, news_m, members_t.ToString(), slots.ToString(), gold.ToString() };

            Query.InsertQuery(this.conn, "guild", columns, values);
        }

        public int getGuildIdByName(string name)
        {
            for (int i = 0; i < this._guild.GetLength(0); i++)
            {
                if (this._guild[i, 1] == name)
                    return i;
            }

            return 0;
        }
    }
}
